//
//  KVWelcomeView.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/22.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class KVWelcomeView: UIView {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.greenColor()
        self.setupSubViews()
        //加载网络数据给头像赋值
//       loadUserInfo()
        //加载网络数据给头像赋值
        loadUserInfo()
    }
    
    private func setupSubViews() {
        self.addSubview(backgroundImgView)
        self.addSubview(iconView)
        self.addSubview(label)
      
        //设置约束
        backgroundImgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self.snp_bottom).offset(-260)
            make.width.height.equalTo(85)
        }
        label.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    private func loadUserInfo() {
        KVUserAccountViewModel.sharedUserAccountViewModel.loadUserBaseInfo { (error) -> Void in
            if error != nil {
                print(error)
            }
            //MARK: - error
            if let avatar_large = KVUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large {
                self.iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage:  UIImage(named: "ad_background"))
         
            }
            
        
        }
    }
    
    // MARK: - 私有方法
    func startAnimataion() {
        iconView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp_bottom).offset(-500)
        }
     
      UIView.animateWithDuration(0.25, delay: defultAnimationTime, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
        self.layoutIfNeeded()
        }) { (_) -> Void in
            UIView.animateWithDuration(defultAnimationTime, animations: { () -> Void in
                self.label.alpha = 1
                print("动画完成回调主页控制器")
            })
            AppDelegate.setupRootViewController(KVBaseTabBarVC())

        }
    }

    // MARK: - 懒加载控件
    private lazy var backgroundImgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView: UIImageView = {
        let iconvew = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconvew.layer.cornerRadius = 42.5
        iconvew.layer.masksToBounds = true
        return iconvew
    }()
    private lazy var label: UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.alpha = 0
        label.sizeToFit()
        return label
    }()

}
